package xiaoka.ecarx.com.cn.upnpserverdemo;

import org.teleal.cling.binding.annotations.*;

import java.util.Map;

/**
 * Created by Chong Zhao on 2016/10/30.
 */
@UpnpService(
        serviceId = @UpnpServiceId("MessageHandler"),
        serviceType = @UpnpServiceType(value = "MessageHandler", version = 1)
)
public class MessageHandler {
    
    @UpnpStateVariable(defaultValue = "")
    private String msg = "";
    
    @UpnpStateVariable(defaultValue = "")
    private String from = "";

    @UpnpAction(out = @UpnpOutputArgument(name = "Ret"))
    public  String setMsg(@UpnpInputArgument(name = "Msg") String newMsg,
                         @UpnpInputArgument(name = "From") String from) {
    	this.msg = newMsg; 
    	this.from = from;
    	System.out.println("receive new msg:" + newMsg);
        MessageController.getInstance().doHandler(newMsg,from);
    	//MainActivity.mInstance.messageReceived(newMsg,from);
    	return "Received";
    }

    public static interface MsgDoHandler{

        public void messageReceived(String msg, String from);
    }


    //
    public static class MessageController{

        static   MessageController mMessageController;
        private MsgDoHandler mHandler;
        private MessageController(){

        }

        public  static MessageController getInstance(){
            if(mMessageController == null){
                mMessageController = new MessageController();
            }
            return mMessageController;
        }

        public void registHandler(MsgDoHandler handler){
            mHandler = handler;
        }

        public  void doHandler(String msg, String from){
            if(mHandler!=null){
                mHandler.messageReceived(msg,from);
            }
        }
    }

}

